<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="姓名">
              <select-user-by-dep
                  v-model="queryParam.employeeId" :multi="true"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="时间(月)">
              <a-month-picker placeholder="请选择时间(月)" style="width: 100%" v-model="queryParam.date" @change="onChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="打卡状态">
                <j-dict-select-tag placeholder="请选择打卡状态" v-model="queryParam.status" dictCode="attendance_status"/>
              </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="所属组织">
              <j-select-depart  v-model="queryParam.departId" multi treeOpera/>
            </a-form-item>
          </a-col>
          <!-- <template v-if="toggleSearchStatus"></template> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus" :disabled="isDisabledAuth('erpEmployeeAttendanceMonth:add')">新增</a-button> -->
      <a-button type="primary" icon="download" @click="handleExportXls()" :disabled="isDisabledAuth('erpEmployeeAttendanceMonth:export')">导出</a-button>
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import" :disabled="isDisabledAuth('erpEmployeeAttendanceMonth:import')">导入</a-button>
      </a-upload> -->
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item, index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
          <a slot="regularWorkCount" slot-scope="text, record" @click="getDay(record,{key:'status',value:1})">{{ text }}</a>
          <a slot="exceptDaysCount" slot-scope="text, record" @click="getDay(record,{key:'status',value:0})">{{ text }}</a>
          <a slot="lateCount" slot-scope="text, record" @click="getDay(record,{key:'isLate',value:1})">{{ text }}</a>
          <a slot="leaveCount" slot-scope="text, record" @click="getDay(record,{key:'isLeave',value:1})">{{ text }}</a>
          <a slot="absentWorkCount" slot-scope="text, record" @click="getDay(record,{key:'isAbsentWork',value:1})">{{ text }}</a>
          <a slot="missingCount" slot-scope="text, record" @click="getDay(record,{key:'isMissing',value:1})">{{ text }}</a>
          <a slot="locationAnomalyCount" slot-scope="text, record" @click="getDay(record,{key:'isLocationAnomaly',value:1})">{{ text }}</a>
          <a slot="unitExceptionCount" slot-scope="text, record" @click="getDay(record,{key:'isUnitException',value:1})">{{ text }}</a>
          <a slot="workdayOverMin" slot-scope="text, record" @click="getDay(record,{key:'isWorkOver',value:1})">{{ text }}</a>
          <a slot="holidaysOverMin" slot-scope="text, record" @click="getDay(record,{key:'isHolidaysOver',value:1})">{{ text }}</a>
          <a slot="restdaysOverMin" slot-scope="text, record" @click="getDay(record,{key:'isRestdaysOver',value:1})">{{ text }}</a>
          <a slot="replenishmentCardCount" slot-scope="text, record" @click="getDay(record,{key:'isReplenishmentCard',value:1})">{{ text }}</a>
          <a slot="fieldWorkCount" slot-scope="text, record" @click="getDay(record,{key:'isFieldWork',value:1})">{{ text }}</a>
          <a slot="outDuration" slot-scope="text, record" @click="getDay(record,{key:'isOut',value:1})">{{ text }}</a>
          <a slot="travelDuration" slot-scope="text, record" @click="getDay(record,{key:'isTravel',value:1})">{{ text }}</a>
          <a slot="annualLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isAnnualLeave',value:1})">{{ text }}</a>
          <a slot="askLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isAskLeave',value:1})">{{ text }}</a>
          <a slot="sickLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isSickLeave',value:1})">{{ text }}</a>
          <a slot="leaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isRestLeave',value:1})">{{ text }}</a>
          <a slot="marriageLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isMarriageLeave',value:1})">{{ text }}</a>
          <a slot="maternityLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isMaternityLeave',value:1})">{{ text }}</a>
          <a slot="accompanyingLaborDuration" slot-scope="text, record" @click="getDay(record,{key:'isAccompanyingLabor',value:1})">{{ text }}</a>
          <a slot="breastfeedingLeaveDuration" slot-scope="text, record" @click="getDay(record,{key:'isBreastfeedingLeave',value:1})">{{ text }}</a>
          <a slot="otherDuration" slot-scope="text, record" @click="getDay(record,{key:'isOther',value:1})">{{ text }}</a>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <!-- <span slot="action" slot-scope="text, record">
            <a-button
                class="ruleButton"
                :ghost="true"
                type="primary"
                icon="edit"
                @click="handleEdit(record)"
                :disabled="isDisabledAuth('erpEmployeeAttendanceMonth:edit')"
        />
        <a-divider type="vertical"/>
        <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="delete"
                    :disabled="isDisabledAuth('erpEmployeeAttendanceMonth:delete')"/>
        </a-popconfirm>
        </span> -->

      </a-table>
    </div>

    <erp-employee-attendance-month-modal ref="modalForm" @ok="modalFormOk"></erp-employee-attendance-month-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpEmployeeAttendanceMonthModal from './modules/ErpEmployeeAttendanceMonthModal'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import { deleteAction, downFile, getAction, getFileAccessHttpUrl } from '@/api/manage'
  import moment from 'moment'

  export default {
    name: 'ErpEmployeeAttendanceMonthList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpEmployeeAttendanceMonthModal,
      SelectUserByDep
    },
    data () {
      return {
        queryParam:{
          date:moment(new Date()).subtract(1,'months').format('YYYY-MM')
        },
        description: '员工考勤月报表管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '基本信息',
            width:600,
            // fixed: 'left',
            dataIndex:"基本信息",
            children: [
            {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
              }
            },
            {
              title:'时间（月）',
              align:"center",
              dataIndex: 'date',
              customRender:function (text) {
                return !text?"":(text.length>10?text.substr(0,10):text)
              }
            },
            {
              title:'姓名',
              align:"center",
              dataIndex: 'employeeId_dictText',
            },
            {
              title:'企业微信账号',
              align:"center",
              dataIndex: 'wxUserId',
            },
            {
              title:'所属组织',
              align:"center",
              dataIndex: 'departId_dictText',
            },
            {
              title:'职务',
              align:"center",
              dataIndex: 'postId_dictText',
            },
            ]
          },
          {
            title: '打卡累计',
            dataIndex:"打卡累计",
            width:400,
            children: [
              {
                  title:'应打卡天数',
                  align:"center",
                  dataIndex: 'workDaysCount',
              },
              {
                title:'正常打卡天数',
                align:"center",
                dataIndex: 'regularWorkCount',
                scopedSlots: { customRender: 'regularWorkCount' }
              },
              {
                title:'异常打卡天数',
                align:"center",
                dataIndex: 'exceptDaysCount',
                scopedSlots: { customRender: 'exceptDaysCount' }
              },
            ]
          },
          {
            title: '异常打卡累计',
            dataIndex:"异常打卡累计",
            width:800,
            children: [
              {
              title:'迟到总次数',
              align:"center",
              dataIndex: 'lateCount',
              scopedSlots: { customRender: 'lateCount' }
            },
            {
              title:'早退总次数',
              align:"center",
              dataIndex: 'leaveCount',
              scopedSlots: { customRender: 'leaveCount' }
            },
            {
              title:'旷工总次数',
              align:"center",
              dataIndex: 'absentWorkCount',
              scopedSlots: { customRender: 'absentWorkCount' }
            },
            {
              title:'缺卡总次数',
              align:"center",
              dataIndex: 'missingCount',
              scopedSlots: { customRender: 'missingCount' }
            },
            {
              title:'地点异常总次数',
              align:"center",
              dataIndex: 'locationAnomalyCount',
              scopedSlots: { customRender: 'locationAnomalyCount' }
            },
            {
              title:'设备异常总次数',
              align:"center",
              dataIndex: 'unitExceptionCount',
              scopedSlots: { customRender: 'unitExceptionCount' }
            },
            ]
          },
          {
            title: '加班情况累计',
            dataIndex:"加班情况累计",
            children: [
              {
                title:'工作日加班(分钟)',
                align:"center",
                dataIndex: 'workdayOverMin',
                scopedSlots: { customRender: 'workdayOverMin' }
              },
              {
                title:'节假日加班(分钟)',
                align:"center",
                dataIndex: 'holidaysOverMin',
                scopedSlots: { customRender: 'holidaysOverMin' }
              },
              {
                title:'休息日加班(分钟)',
                align:"center",
                dataIndex: 'restdaysOverMin',
                scopedSlots: { customRender: 'restdaysOverMin' }
              },
            ]
          },
          {
            title: '假勤情况累计',
            dataIndex:"假勤情况累计",
            children: [
              {
                title:'补卡次数',
                align:"center",
                dataIndex: 'replenishmentCardCount',
                scopedSlots: { customRender: 'replenishmentCardCount' }
              },
              {
                title:'外勤次数',
                align:"center",
                dataIndex: 'fieldWorkCount',
                scopedSlots: { customRender: 'fieldWorkCount' }
              },
              {
                title:'外出小时',
                align:"center",
                dataIndex: 'outDuration',
                scopedSlots: { customRender: 'outDuration' }
              },
              {
                title:'出差天数',
                align:"center",
                dataIndex: 'travelDuration',
                scopedSlots: { customRender: 'travelDuration' }
              },
              {
                title:'年假天数',
                align:"center",
                dataIndex: 'annualLeaveDuration',
                scopedSlots: { customRender: 'annualLeaveDuration' }
              },
              {
                title:'事假(小时)',
                align:"center",
                dataIndex: 'askLeaveDuration',
                scopedSlots: { customRender: 'askLeaveDuration' }
              },
              {
                title:'病假(小时)',
                align:"center",
                dataIndex: 'sickLeaveDuration',
                scopedSlots: { customRender: 'sickLeaveDuration' }
              },
              {
                title:'调休假(小时)',
                align:"center",
                dataIndex: 'leaveDuration',
                scopedSlots: { customRender: 'leaveDuration' }
              },
              {
                title:'婚假天数',
                align:"center",
                dataIndex: 'marriageLeaveDuration',
                scopedSlots: { customRender: 'marriageLeaveDuration' }
              },
              {
                title:'产假天数',
                align:"center",
                dataIndex: 'maternityLeaveDuration',
                scopedSlots: { customRender: 'maternityLeaveDuration' }
              },
              {
                title:'陪产假天数',
                align:"center",
                dataIndex: 'accompanyingLaborDuration',
                scopedSlots: { customRender: 'accompanyingLaborDuration' }
              },
              {
                title:'哺乳假天数',
                align:"center",
                dataIndex: 'breastfeedingLeaveDuration',
                scopedSlots: { customRender: 'breastfeedingLeaveDuration' }
              },
              {
                title:'其他天数',
                align:"center",
                dataIndex: 'otherDuration',
                scopedSlots: { customRender: 'otherDuration' }
              },
            ]
          },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   fixed:"right",
          //   width:147,
          //   scopedSlots: {
          //   filterDropdown: 'filterDropdown',
          //   filterIcon: 'filterIcon',
          //   customRender: 'action' }
          // }
        ],
        url: {
          list: "/api/erp/hr/attendance/erpEmployeeAttendanceMonth/list",
          delete: "/api/erp/hr/attendance/erpEmployeeAttendanceMonth/delete",
          deleteBatch: "/api/erp/hr/attendance/erpEmployeeAttendanceMonth/deleteBatch",
          exportXlsUrl: "/api/erp/hr/attendance/erpEmployeeAttendanceMonth/exportXls",
          importExcelUrl: "api/erp/hr/attendance/erpEmployeeAttendanceMonth/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      searchReset() {
        this.queryParam = {}
        this.queryParam.date=moment(new Date()).subtract(1,'months').format('YYYY-MM');
        this.loadData(1)
      },
      handleExportXls() {
      let fileName = "员工考勤月报表-"+this.queryParam.date;
      let param = this.getQueryParams()
      if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
        param['selections'] = this.selectedRowKeys.join(',')
      }
      downFile(this.url.exportXlsUrl, param).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(
            new Blob([data], {
              type: 'application/vnd.ms-excel'
            }),
            fileName + '.xls'
          )
        } else {
          let url = window.URL.createObjectURL(
            new Blob([data], {
              type: 'application/vnd.ms-excel'
            })
          )
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },
      //查看日报信息
      getDay(record,obj){
        let params= {monthId: record.id,employeeId:record.employeeId,dayDate:record.date}
        params[obj.key]=obj.value
        this.$router.push({
          path: '/erp/hr/attendance/ErpEmployeeAttendanceDayRecordList',
          query: params
        })
      },
      onChange(date, dateString) {
        this.queryParam.date = dateString
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'date',value:'date',text:'时间（月）'})
        fieldList.push({type:'string',value:'employeeId',text:'员工名称',dictCode:''})
        fieldList.push({type:'string',value:'departId',text:'所属组织',dictCode:''})
        fieldList.push({type:'string',value:'postId',text:'职务',dictCode:''})
        fieldList.push({type:'int',value:'regularWorkCount',text:'正常打开次数',dictCode:''})
        fieldList.push({type:'int',value:'lateCount',text:'迟到次数',dictCode:''})
        fieldList.push({type:'int',value:'leaveCount',text:'早退次数',dictCode:''})
        fieldList.push({type:'int',value:'absentWorkCount',text:'旷工次数',dictCode:''})
        fieldList.push({type:'int',value:'missingCount',text:'缺卡次数',dictCode:''})
        fieldList.push({type:'int',value:'locationAnomalyCount',text:'地点异常',dictCode:''})
        fieldList.push({type:'int',value:'unitExceptionCount',text:'设备异常',dictCode:''})
        fieldList.push({type:'int',value:'workdayOverMin',text:'工作日加班(分钟)',dictCode:''})
        fieldList.push({type:'int',value:'holidaysOverMin',text:'节假日加班(分钟)',dictCode:''})
        fieldList.push({type:'int',value:'restdaysOverMin',text:'休息日加班(分钟)',dictCode:''})
        fieldList.push({type:'int',value:'replenishmentCardCount',text:'补卡次数',dictCode:''})
        fieldList.push({type:'int',value:'fieldWorkCount',text:'外勤次数',dictCode:''})
        fieldList.push({type:'int',value:'outDuration',text:'外出小时',dictCode:''})
        fieldList.push({type:'int',value:'travelDuration',text:'出差天数',dictCode:''})
        fieldList.push({type:'int',value:'annualLeaveDuration',text:'年假天数',dictCode:''})
        fieldList.push({type:'int',value:'askLeaveDuration',text:'事假(小时)',dictCode:''})
        fieldList.push({type:'int',value:'sickLeaveDuration',text:'病假(小时)',dictCode:''})
        fieldList.push({type:'int',value:'leaveDuration',text:'调休假(小时)',dictCode:''})
        fieldList.push({type:'int',value:'marriageLeaveDuration',text:'婚假天数',dictCode:''})
        fieldList.push({type:'int',value:'maternityLeaveDuration',text:'产假天数',dictCode:''})
        fieldList.push({type:'int',value:'accompanyingLaborDuration',text:'陪产假天数',dictCode:''})
        fieldList.push({type:'int',value:'breastfeedingLeaveDuration',text:'哺乳假天数',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
